{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 3\n",
    "# Predicting Sports Winners with Decision Trees"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "data_filename = \"basketball.csv\"\n",
    "dataset = pd.read_csv(data_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": "              Date Start (ET)       Visitor/Neutral  PTS  \\\n0  Tue Oct 27 2015    8:00 pm       Detroit Pistons  106   \n1  Tue Oct 27 2015    8:00 pm   Cleveland Cavaliers   95   \n2  Tue Oct 27 2015   10:30 pm  New Orleans Pelicans   95   \n3  Wed Oct 28 2015    7:30 pm    Philadelphia 76ers   95   \n4  Wed Oct 28 2015    7:30 pm         Chicago Bulls  115   \n\n            Home/Neutral  PTS.1 Unnamed: 6 Unnamed: 7  Attend. Notes  \n0          Atlanta Hawks     94  Box Score        NaN    19187   NaN  \n1          Chicago Bulls     97  Box Score        NaN    21957   NaN  \n2  Golden State Warriors    111  Box Score        NaN    19596   NaN  \n3         Boston Celtics    112  Box Score        NaN    18624   NaN  \n4          Brooklyn Nets    100  Box Score        NaN    17732   NaN  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Date</th>\n      <th>Start (ET)</th>\n      <th>Visitor/Neutral</th>\n      <th>PTS</th>\n      <th>Home/Neutral</th>\n      <th>PTS.1</th>\n      <th>Unnamed: 6</th>\n      <th>Unnamed: 7</th>\n      <th>Attend.</th>\n      <th>Notes</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>Tue Oct 27 2015</td>\n      <td>8:00 pm</td>\n      <td>Detroit Pistons</td>\n      <td>106</td>\n      <td>Atlanta Hawks</td>\n      <td>94</td>\n      <td>Box Score</td>\n      <td>NaN</td>\n      <td>19187</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>Tue Oct 27 2015</td>\n      <td>8:00 pm</td>\n      <td>Cleveland Cavaliers</td>\n      <td>95</td>\n      <td>Chicago Bulls</td>\n      <td>97</td>\n      <td>Box Score</td>\n      <td>NaN</td>\n      <td>21957</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>Tue Oct 27 2015</td>\n      <td>10:30 pm</td>\n      <td>New Orleans Pelicans</td>\n      <td>95</td>\n      <td>Golden State Warriors</td>\n      <td>111</td>\n      <td>Box Score</td>\n      <td>NaN</td>\n      <td>19596</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>Wed Oct 28 2015</td>\n      <td>7:30 pm</td>\n      <td>Philadelphia 76ers</td>\n      <td>95</td>\n      <td>Boston Celtics</td>\n      <td>112</td>\n      <td>Box Score</td>\n      <td>NaN</td>\n      <td>18624</td>\n      <td>NaN</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>Wed Oct 28 2015</td>\n      <td>7:30 pm</td>\n      <td>Chicago Bulls</td>\n      <td>115</td>\n      <td>Brooklyn Nets</td>\n      <td>100</td>\n      <td>Box Score</td>\n      <td>NaN</td>\n      <td>17732</td>\n      <td>NaN</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Length mismatch: Expected axis has 10 elements, new values have 9 elements",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-9-63889738dbb4>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[0mdataset\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mpd\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mread_csv\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mdata_filename\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mparse_dates\u001B[0m\u001B[1;33m=\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;34m\"Date\"\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 3\u001B[1;33m \u001B[0mdataset\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mcolumns\u001B[0m \u001B[1;33m=\u001B[0m \u001B[1;33m[\u001B[0m\u001B[1;34m\"Date\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"Start (ET)\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"Visitor Team\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"VisitorPts\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"Home Team\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"HomePts\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"OT?\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"Score Type\"\u001B[0m\u001B[1;33m,\u001B[0m \u001B[1;34m\"Notes\"\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m      4\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001B[0m in \u001B[0;36m__setattr__\u001B[1;34m(self, name, value)\u001B[0m\n\u001B[0;32m   5476\u001B[0m         \u001B[1;32mtry\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   5477\u001B[0m             \u001B[0mobject\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m__getattribute__\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mname\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m-> 5478\u001B[1;33m             \u001B[1;32mreturn\u001B[0m \u001B[0mobject\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m__setattr__\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mname\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mvalue\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m   5479\u001B[0m         \u001B[1;32mexcept\u001B[0m \u001B[0mAttributeError\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m   5480\u001B[0m             \u001B[1;32mpass\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mpandas\\_libs\\properties.pyx\u001B[0m in \u001B[0;36mpandas._libs.properties.AxisProperty.__set__\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001B[0m in \u001B[0;36m_set_axis\u001B[1;34m(self, axis, labels)\u001B[0m\n\u001B[0;32m    668\u001B[0m     \u001B[1;32mdef\u001B[0m \u001B[0m_set_axis\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mself\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0maxis\u001B[0m\u001B[1;33m:\u001B[0m \u001B[0mint\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mlabels\u001B[0m\u001B[1;33m:\u001B[0m \u001B[0mIndex\u001B[0m\u001B[1;33m)\u001B[0m \u001B[1;33m->\u001B[0m \u001B[1;32mNone\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    669\u001B[0m         \u001B[0mlabels\u001B[0m \u001B[1;33m=\u001B[0m \u001B[0mensure_index\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mlabels\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 670\u001B[1;33m         \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_mgr\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mset_axis\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0maxis\u001B[0m\u001B[1;33m,\u001B[0m \u001B[0mlabels\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m    671\u001B[0m         \u001B[0mself\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0m_clear_item_cache\u001B[0m\u001B[1;33m(\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    672\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\managers.py\u001B[0m in \u001B[0;36mset_axis\u001B[1;34m(self, axis, new_labels)\u001B[0m\n\u001B[0;32m    218\u001B[0m \u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    219\u001B[0m         \u001B[1;32mif\u001B[0m \u001B[0mnew_len\u001B[0m \u001B[1;33m!=\u001B[0m \u001B[0mold_len\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m--> 220\u001B[1;33m             raise ValueError(\n\u001B[0m\u001B[0;32m    221\u001B[0m                 \u001B[1;34mf\"Length mismatch: Expected axis has {old_len} elements, new \"\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m    222\u001B[0m                 \u001B[1;34mf\"values have {new_len} elements\"\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mValueError\u001B[0m: Length mismatch: Expected axis has 10 elements, new values have 9 elements"
     ]
    }
   ],
   "source": [
    "dataset = pd.read_csv(data_filename, parse_dates=[\"Date\"])\n",
    "\n",
    "dataset.columns = [\"Date\", \"Start (ET)\", \"Visitor Team\", \"VisitorPts\", \"Home Team\", \"HomePts\", \"OT?\", \"Score Type\", \"Notes\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(dataset.dtypes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dataset[\"HomeWin\"] = dataset[\"VisitorPts\"] < dataset[\"HomePts\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "y_true = dataset[\"HomeWin\"].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset[\"HomeWin\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "won_last = defaultdict(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dataset[\"HomeLastWin\"] = 0\n",
    "dataset[\"VisitorLastWin\"] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    row[\"HomeLastWin\"] = won_last[home_team]\n",
    "    dataset.set_value(index, \"HomeLastWin\", won_last[home_team])\n",
    "    dataset.set_value(index, \"VisitorLastWin\", won_last[visitor_team])\n",
    "    \n",
    "    won_last[home_team] = int(row[\"HomeWin\"])\n",
    "    won_last[visitor_team] = 1 - int(row[\"HomeWin\"])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset.head(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset.ix[1000:1005]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_previouswins = dataset[[\"HomeLastWin\", \"VisitorLastWin\"]].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "clf = DecisionTreeClassifier(random_state=14)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#from sklearn.cross_validation import cross_val_score\n",
    "from sklearn.model_selection import cross_val_score\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "scores = cross_val_score(clf, X_previouswins, y_true,\n",
    "scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "standings_filename = os.path.join(\"standings.csv\")\n",
    "\n",
    "standings = pd.read_csv(standings_filename, skiprows=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "standings.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset[\"HomeTeamRanksHigher\"] = 0\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    home_rank = standings[standings[\"Team\"] == home_team][\"Rk\"].values[0]\n",
    "    visitor_rank = standings[standings[\"Team\"] == visitor_team][\"Rk\"].values[0]\n",
    "    dataset.set_value(index, \"HomeTeamRanksHigher\", int(home_rank < visitor_rank))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X_homehigher = dataset[[ \"HomeTeamRanksHigher\", \"HomeLastWin\", \"VisitorLastWin\",]].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "clf = DecisionTreeClassifier(random_state=14, criterion=\"entropy\")\n",
    "\n",
    "scores = cross_val_score(clf, X_homehigher, y_true, scoring='accuracy')\n",
    "\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "last_match_winner = defaultdict(int)\n",
    "dataset[\"HomeTeamWonLast\"] = 0\n",
    "\n",
    "for index, row in dataset.iterrows():\n",
    "    home_team = row[\"Home Team\"]\n",
    "    visitor_team = row[\"Visitor Team\"]\n",
    "    teams = tuple(sorted([home_team, visitor_team]))  # Sort for a consistent ordering\n",
    "    # Set in the row, who won the last encounter\n",
    "    home_team_won_last = 1 if last_match_winner[teams] == row[\"Home Team\"] else 0\n",
    "    dataset.set_value(index, \"HomeTeamWonLast\", home_team_won_last)\n",
    "    # Who won this one?\n",
    "    winner = row[\"Home Team\"] if row[\"HomeWin\"] else row[\"Visitor Team\"]\n",
    "    last_match_winner[teams] = winner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dataset.ix[400:450]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X_lastwinner = dataset[[ \"HomeTeamWonLast\", \"HomeTeamRanksHigher\", \"HomeLastWin\", \"VisitorLastWin\",]].values\n",
    "clf = DecisionTreeClassifier(random_state=14, criterion=\"entropy\")\n",
    "\n",
    "scores = cross_val_score(clf, X_lastwinner, y_true, scoring='accuracy')\n",
    "\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import LabelEncoder\n",
    "encoding = LabelEncoder()\n",
    "encoding.fit(dataset[\"Home Team\"].values)\n",
    "home_teams = encoding.transform(dataset[\"Home Team\"].values)\n",
    "visitor_teams = encoding.transform(dataset[\"Visitor Team\"].values)\n",
    "X_teams = np.vstack([home_teams, visitor_teams]).T\n",
    "\n",
    "from sklearn.preprocessing import OneHotEncoder\n",
    "onehot = OneHotEncoder()\n",
    "X_teams = onehot.fit_transform(X_teams).todense()\n",
    "\n",
    "clf = DecisionTreeClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_teams, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X_all = np.hstack([X_lastwinner, X_teams])\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "scores = cross_val_score(clf, X_all, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X_all = np.hstack([X_lastwinner, X_teams])\n",
    "clf = RandomForestClassifier(random_state=14, n_estimators=250)\n",
    "scores = cross_val_score(clf, X_all, y_true, scoring='accuracy')\n",
    "print(\"Accuracy: {0:.1f}%\".format(np.mean(scores) * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#from sklearn.grid_search import GridSearchCV\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "parameter_space = {\n",
    "    \"max_features\": [2, 10, 'auto'],\n",
    "    \"n_estimators\": [100, 200],\n",
    "    \"criterion\": [\"gini\", \"entropy\"],\n",
    "    \"min_samples_leaf\": [2, 4, 6],\n",
    "}\n",
    "clf = RandomForestClassifier(random_state=14)\n",
    "grid = GridSearchCV(clf, parameter_space)\n",
    "grid.fit(X_all, y_true)\n",
    "print(\"Accuracy: {0:.1f}%\".format(grid.best_score_ * 100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(grid.best_estimator_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}